﻿Imports System.IO

Public Class frmInsertarMision
    'Los siguientes atributos los hice como una opcion para convertir
    'la imagen a binario desde la UI y no desde la capa de datos.
    'no esta implementado asi, es solo para fines didacticos.
    Private openFileDialog1 As System.Windows.Forms.OpenFileDialog = New OpenFileDialog()
    Private pictureBox1 As System.Windows.Forms.PictureBox
    Private m_lImageFileLength As Long = 0
    Private m_barrImg As Byte()

    Private Sub btnSalir_Click(sender As Object, e As EventArgs) Handles btnSalir.Click
        Me.Close()
    End Sub

    Private Sub frmInsertarMision_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        ModuloGeneral.Centrar(Me.Panel1)
        Me.picInsignia.Image = Nothing
    End Sub

    Private Sub frmInsertarMision_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.cmbPrograma.DataSource = New BindingSource(objGestor.listarProgramaInfo(), Nothing)
        Me.cmbPrograma.DisplayMember = "Value"
        Me.cmbPrograma.ValueMember = "Key"

        Dim listaCC As New Dictionary(Of Integer, String)()
        listaCC = objGestor.listarCuerposCelestesInfo()

        Me.cmbDestino.DataSource = New BindingSource(listaCC, Nothing)
        Me.cmbDestino.DisplayMember = "Value"
        Me.cmbDestino.ValueMember = "Key"
    End Sub

    Private Sub LimpiarCampos()

    End Sub

    Private Sub btnBuscarPic_Click(sender As Object, e As EventArgs) Handles btnBuscarPic.Click
        Me.openFileDialog1.Filter = "All Files|*.*|BMP Files|*.bmp|JPG Files|*.jpg"
        Me.openFileDialog1.ShowDialog(Me)

        Dim strFn As String = Me.openFileDialog1.FileName
        If strFn <> "" Then
            Me.picInsignia.Image = Image.FromFile(strFn)

            Dim fiImage As New FileInfo(strFn)
            Me.m_lImageFileLength = fiImage.Length
            Dim fs As New FileStream(strFn, FileMode.Open, FileAccess.Read, FileShare.Read)
            m_barrImg = New Byte(Convert.ToInt32(Me.m_lImageFileLength) - 1) {}
            Dim iBytesRead As Integer = fs.Read(m_barrImg, 0, Convert.ToInt32(Me.m_lImageFileLength))
            fs.Close()
        End If

    End Sub

    Private Sub btnBorrarPic_Click(sender As Object, e As EventArgs) Handles btnBorrarPic.Click
        Me.picInsignia.Image = Nothing
        Me.m_lImageFileLength = 0
        m_barrImg = {}
    End Sub

    Private Sub btnRegistrar_Click(sender As Object, e As EventArgs) Handles btnRegistrar.Click
        Dim Validor As New ValidadorDeFormularios(Me, ErrorProvider1)
        Dim FormularioValido As Boolean = Validor.ValidarFormulario()

        If FormularioValido = False Then
            Return
        End If

        Dim nombre As String = txtString1.Text
        Dim tripul As String = txtString2.Text
        Dim fechaLanza As Date = Convert.ToDateTime(dptFechaLanza.Text)
        Dim duracion As TimeSpan = New TimeSpan(Convert.ToDecimal(Me.txtDecimal1.Text) * 24, 0, 0)
        Dim datosInt As String = txtString3.Text
        Dim result As String = txtString5.Text
        Dim insignia As Image = Me.picInsignia.Image
        Dim nave As String = Me.txtString4.Text
        Dim IdPrograma As Integer = DirectCast(Me.cmbPrograma.SelectedItem, KeyValuePair(Of Integer, String)).Key
        Dim IdDestino As Integer = DirectCast(Me.cmbDestino.SelectedItem, KeyValuePair(Of Integer, String)).Key
        'el campo binario de la imagen a enviar seria: m_barrImg 

        If objGestor.insertarMission(nombre, tripul, fechaLanza, duracion, datosInt, result, insignia, nave, IdDestino, IdPrograma) Then
            MsgBox("Misión Registrada Correctamente!")
            Me.LimpiarCampos()
        Else
            MsgBox("La Misión No fue Registrada.!")
        End If
    End Sub

    Private Sub cmbDestino_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDestino.SelectedIndexChanged

    End Sub

    Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub picInsignia_Click(sender As Object, e As EventArgs) Handles picInsignia.Click

    End Sub
End Class